package es.mgil.blog;

import java.io.IOException;
import java.io.PrintWriter;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class DelBlogServletD extends HttpServlet {
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		PrintWriter out = resp.getWriter();

		String id = req.getParameter("id");
		if (id == null) {
			out.println("Missing message ID");
			return;
		}

		long idL = -1;
		try {
			idL = Long.parseLong(id);
		} catch (Exception e) {
			out.println("Introduced ID is not a valid number");
			return;
		}

		PersistenceManager pm = PMF.get().getPersistenceManager();
		Mensaje msg = null;
		try {
			msg = pm.getObjectById(Mensaje.class, idL);
		} catch (Exception e) {
			out.println("Could not find a message with this ID");
			return;
		}

		try {
			pm.deletePersistent(msg);
		} finally {
			pm.close();
		}

		resp.sendRedirect("/borrar");
		resp.setCharacterEncoding("utf-8");
	}
}
